﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.BLL;
using Wp.DAL;

public partial class Admin_Modules_Posts_Controls_CategoryList : System.Web.UI.UserControl
{
    public static string Taxonomy = "category";
    public int PostId { get; set; }
    public List<TblTermRelationshipsEntity> ls { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    LoadData();
        //}
    }
    public void LoadData(int iPostId)
    {
        PostId = iPostId;
        ls = TblTermRelationshipsManager.CreateInstant().SelectByObjectId(PostId, Taxonomy);


    }
    public void LoadData()
    {

        rpt.DataSource = TblTermTaxonomyManager.CreateInstant().SelectTblTermsTaxonomyGetChildFormat(null, Taxonomy);
        rpt.DataBind();

    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox cb = (CheckBox)e.Item.FindControl("cb");
            HiddenField hdf = (HiddenField)e.Item.FindControl("hdf");
            if (ls != null && ls.Where(n => n.TermTaxonomyId == hdf.Value.ToInt()).Count() > 0)
            {
                cb.Checked = true;
            }
        }
    }
    public void Update(int PostId)
    {
        DataTable dt = new DataTable();
        DataColumn column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.AllowDBNull = false;
        column.Caption = "ObjectId";
        column.ColumnName = "ObjectId";
        dt.Columns.Add(column);
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.AllowDBNull = false;
        column.Caption = "TermTaxonomyId";
        column.ColumnName = "TermTaxonomyId";
        dt.Columns.Add(column);
        bool ChangeValue = false;
        for (int i = 0; i < rpt.Items.Count; i++)
        {
            CheckBox cb = (CheckBox)rpt.Items[i].FindControl("cb");
            HiddenField hdf = (HiddenField)rpt.Items[i].FindControl("hdf");
            if (cb.Checked)
            {
                DataRow r = dt.NewRow();
                r["ObjectId"] = PostId;
                r["TermTaxonomyId"] = hdf.Value.ToInt();
                dt.Rows.Add(r);
                ChangeValue = true;
            }
        }
        dt.AcceptChanges();
        if (ChangeValue)
            TblTermRelationshipsManager.CreateInstant().Update(dt);
    }

}